package com.atguigu.userprofile.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.userprofile.bean.TagCondition;
import com.atguigu.userprofile.bean.TaskInfo;
import com.atguigu.userprofile.bean.UserGroup;
import com.atguigu.userprofile.service.UserGroupService;
import com.atguigu.userprofile.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import javafx.scene.chart.ValueAxis;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2021-05-04
 */
@RestController
public class UserGroupController {

    @Autowired
    UserGroupService userGroupService;

    @RequestMapping("/user-group-list")
    @CrossOrigin
    public String  getUserGroupList(@RequestParam("pageNo")int pageNo , @RequestParam("pageSize") int pageSize){
        int startNo=(  pageNo-1)* pageSize;
        int endNo=startNo+pageSize;

        QueryWrapper<UserGroup> queryWrapper = new QueryWrapper<>();
        int count = userGroupService.count(queryWrapper);

        queryWrapper.orderByDesc("id").last(" limit " + startNo + "," + endNo);
        List<UserGroup> userGroupList = userGroupService.list(queryWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("detail",userGroupList);
        jsonObject.put("total",count);

        return  jsonObject.toJSONString();
    }

    /*
            接收json格式的参数。
                在方法的形参位置声明Map或Bean

             clickhouse不适合 高 QPS的查询，redis适合高 QPS 查询且适合当缓存。
     */
    @PostMapping(value = "/user-group")
    public Object userGroup(@RequestBody UserGroup userGroup){

        //把数据进行处理，存入Mysql。只有这样才能获取到分群的id。写入mysql，此时id会自动回传给程序，赋值给userGroup
        userGroupService.saveUserGroup(userGroup);
        //有了分群id后，可以计算分群包，将分群包结果写入ck
        userGroupService.writeUserGroupToCk(userGroup);
        //把人群包同步到redis一份，让业务系统后续查询redis缓存,获取userGroupNum
        int userGroupNum = userGroupService.writeUserGroupToRedis(userGroup);
        //更新mysql中的元数据，写入userGroupNum
        userGroup.setUserGroupNum(userGroupNum+0l);
        userGroupService.saveOrUpdate(userGroup);
        return "ok";

    }

    @RequestMapping(value = "/user-group-evaluate")
    public Object evaluateUserGroupNum(@RequestBody UserGroup userGroup){

        //根据分群信息，计算人数
        int num = userGroupService.evaluateUserGroup(userGroup);

        //返回结果
        return num;

    }

    @RequestMapping(value = "/user-group-refresh/{id}")
    public Object updateUserGroup(String busiDate,@PathVariable("id") String id){

        //根据id查询mysql中的 元数据信息
        UserGroup userGroup = userGroupService.getById(id);

        //根据查询到的条件，重新计算。计算之前，需要为查询条件和业务日期属性赋值
        List<TagCondition> tagConditions = JSON.parseArray(userGroup.getConditionJsonStr(), TagCondition.class);
        userGroup.setBusiDate(busiDate);
        userGroup.setTagConditions(tagConditions);

        //更新ck
        userGroupService.updateUserGroupToCk(userGroup);
        //更新redis
        int num = userGroupService.writeUserGroupToRedis(userGroup);
        //修改更新后的人群包的人数
        userGroup.setUserGroupNum(num+0l);
        userGroup.setUpdateTime(new Date());
        //将元数据更新
        userGroupService.saveOrUpdate(userGroup);
        //返回结果
        return "ok";

    }




}

